package com.foot.travel.fragment;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.widget.Toast;

import com.koalac.ironman.common.adapter.CommonAdapter;
import com.koalac.ironman.common.module.ModuleActivity.LoadState;
import com.koalac.ironman.net.MyRequest;
import com.koalac.ironman.net.ResponseCallback;
import com.koalac.ironman.net.pojo.request.ClientProtocolListBase;
import com.koalac.ironman.net.pojo.response.BaseResponse;
import com.koalac.ironman.net.pojo.response.Response;
import com.koalac.ironman.util.StringUtils;
import com.koalac.ironman.widget.LoadingDialog;
import com.koalac.ironman.widget.PullToRefreshView;

public class ModuleFragment extends Fragment{

	protected int page = 1;
	protected final int perPage = 20;
	protected LoadingDialog p = null;
	
	/**
	 * 绑定年月日的数据
	 */
	protected Bundle mYMDData = null;
	public String mUnReadNum = "0";
	
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}
	
	/**
	 * 加载数据
	 */
	public <T extends BaseResponse> void loadData(final Class<T> clazz,
			final LoadState loadState, final ClientProtocolListBase params,
			final PullToRefreshView container, final CommonAdapter<T> adapter,
			final List<T> list) {
		/**
		 * 无网络时的UI表现
		 */
		if(!MyRequest.checkNetWork(getActivity())){
			Toast.makeText(getActivity(), "当前无网络,请检查网络后再试!", Toast.LENGTH_SHORT).show();
			container.onHeaderRefreshComplete();
			container.onFooterRefreshComplete();
			return;
		}
		if (loadState == LoadState.init) {
			page = 0;
			p = showLoadingDialog();
		} else if (loadState == LoadState.refresh) {
			page = 0;
		} else if (loadState == LoadState.more) {

		}
		params.setPage(page);
		params.setPerpage(perPage);
		/**
		 * 保存到NetUIManager类中
		 */
		container.setTag(loadState);
		
		MyRequest.request(getActivity(), params, new ResponseCallback() {
			@Override
			public void process(boolean result, Context context, Response data,
					String msg) {
				Toast toast = null;
				if (result) {
					List<T> list = data.getList(clazz);
					if (list != null && list.size() > 0) {
						if (loadState == LoadState.init) {
							adapter.clear();
						} else if (loadState == LoadState.refresh) {
							adapter.clear();
						} else if (loadState == LoadState.more) {
						}
						page += params.getPerpage();
						adapter.addAll(list);
						adapter.notifyDataSetChanged();
					} else {
						if (loadState == LoadState.init) {
							list.clear();
							p.cancel();
							toast = Toast.makeText(context, "没有数据", 3);
						} else if (loadState == LoadState.refresh) {
							list.clear();
							p.cancel();
							container.onHeaderRefreshComplete();
							toast = Toast.makeText(context, "没有数据", 3);
						} else if (loadState == LoadState.more) {
							p.cancel();
							container.onFooterRefreshComplete();
							toast = Toast.makeText(context, "没有更多数据", 3);
						}
						toast.show();
					}
				}else {
					toast = Toast.makeText(context, "数据异常，请稍后再试", 3);
					toast.show();
				}
				if (loadState == LoadState.init) {
					cancelLoadingDialog(p);
				} else if (loadState == LoadState.refresh) {
					container.onHeaderRefreshComplete();
				} else if (loadState == LoadState.more) {
					container.onFooterRefreshComplete();
				}
			}
		});
	}
	
	@Override
	public void onStop() {
		super.onStop();
		if (p!=null) {
			p.cancel();
		}
	}
	
	/**
	 * 显示加载框
	 */
	public LoadingDialog showLoadingDialog(String text) {
		LoadingDialog loadingDialog = LoadingDialog.createLoadingDialog(getActivity(), "", text);
		loadingDialog.show();
		return loadingDialog;
		
	}
	/**
	 * 显示默认加载框
	 */
	public LoadingDialog showLoadingDialog() {
		LoadingDialog loadingDialog = LoadingDialog.createLoadingDialog(getActivity(), "", "正在加载...");
		loadingDialog.show();
		return loadingDialog;
		
	}

	/**
	 * 取消加载框
	 */
	public void cancelLoadingDialog(LoadingDialog loadingDialog) {
		if (loadingDialog != null) {
			loadingDialog.cancel();
		}
	}
	

	protected void toast(String text){
		Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
	}
	
	
}
